package com.nbsaas.boot.shopArticle.ext.resource;

import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.shopArticle.api.apis.ShopArticleApi;
import com.nbsaas.boot.shopArticle.api.apis.ShopArticleDocumentApi;
import com.nbsaas.boot.shopArticle.api.domain.request.ShopArticleDocumentRequest;
import com.nbsaas.boot.shopArticle.api.domain.response.ShopArticleDocumentResponse;
import com.nbsaas.boot.shopArticle.api.domain.response.ShopArticleResponse;
import com.nbsaas.boot.shopArticle.data.entity.ShopArticle;
import com.nbsaas.boot.shopArticle.data.entity.ShopArticleDocument;
import com.nbsaas.boot.shopArticle.data.repository.ShopArticleDocumentRepository;
import com.nbsaas.boot.shopArticle.data.repository.ShopArticleRepository;
import com.nbsaas.boot.shopArticle.ext.apis.ShopArticleExtApi;
import com.nbsaas.boot.shopArticle.ext.domain.request.ShopArticleExtRequest;
import com.nbsaas.boot.shopArticle.ext.domain.response.ShopArticleExtResponse;
import com.nbsaas.boot.shopArticle.rest.convert.ShopArticleEntityConvert;
import com.nbsaas.boot.utils.BeanDataUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class ShopArticleExtResource implements ShopArticleExtApi {

    @Resource
    private ShopArticleApi shopArticleApi;

    @Resource
    private ShopArticleRepository shopArticleRepository;

    @Resource
    private ShopArticleDocumentRepository shopArticleDocumentRepository;

    @Resource
    private ShopArticleDocumentApi shopArticleDocumentApi;

    @Transactional
    @Override
    public ResponseObject<ShopArticleExtResponse> create(ShopArticleExtRequest request) {
        ResponseObject<ShopArticleExtResponse> result = new ResponseObject<>();

        ShopArticleDocumentRequest document=new ShopArticleDocumentRequest();
        document.setNote(request.getNote());
        ShopArticleDocumentResponse documentRes = shopArticleDocumentApi.createData(document);
        request.setDocument(documentRes.getId());
        ResponseObject<ShopArticleResponse> res = shopArticleApi.create(request);
        if (res.getCode()!=200){
            result.setCode(res.getCode());
            result.setMsg(res.getMsg());
            return result;
        }
        ShopArticleExtResponse response=new ShopArticleExtResponse();
        BeanUtils.copyProperties(res.getData(),response);
        response.setNote(documentRes.getNote());

        result.setData(response);
        return result;

    }

    @Transactional
    @Override
    public ResponseObject<ShopArticleExtResponse> update(ShopArticleExtRequest request) {
        ResponseObject<ShopArticleExtResponse> result = new ResponseObject<>();
        Optional<ShopArticle> article = shopArticleRepository.findById(request.getId());
        if (!article.isPresent()) {
            result.setCode(501);
            result.setMsg("无效id");
            return result;
        }
        ShopArticle bean = article.get();
        ShopArticleEntityConvert entityConvert=new ShopArticleEntityConvert();
        ShopArticle entity = entityConvert.convert(request);
        BeanDataUtils.copyProperties(entity, bean);
        ShopArticleDocument document = bean.getDocument();
        if (document==null){
            document=new ShopArticleDocument();
            document.setNote(request.getNote());
            shopArticleDocumentRepository.save(document);
            bean.setDocument(document);
        }else{
            Optional<ShopArticleDocument> temp = shopArticleDocumentRepository.findById(document.getId());
            temp.ifPresent(shopArticleDocument -> shopArticleDocument.setNote(request.getNote()));
        }

        return result;
    }

}
